package com.example.myapp;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

    private Button button;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        button = findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @SuppressLint({"ObsoleteSdkInt", "NotificationPermission"})
            @Override
            public void onClick(View v) {
                String ContentTitle = "通知栏标题演示，我是小雨";
                String ContentText = "通知栏标正文演示,我是孙健雨";
                int ID = 100;//设置1或者2都可以
                //调用系统服务getSystemService
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                Notification notification;//声明Notification对象
                //对不同版本的通知栏有不同的方法，所以弄一个版本控制是那个版本的
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
                    //判断是不是版本号大于26
                    //当SDK版本号大于26（安卓7.0以后得版本）大于哦版本的，也就是大于26版本的
                    String id = "MyService";
                    String description = "my-service";//描述
                    int importance = NotificationManager.IMPORTANCE_LOW;//声明重要性（IMPORTANCE_LOW这个优先级可以设置高或者低）
                    //Notification给他设置ID，名字，重要性（优先级）
                    NotificationChannel channel = new NotificationChannel(id,description,importance);
                    manager.createNotificationChannel(channel);//创建通知的信道

                    //对这个通知进行设置
                    notification = new Notification.Builder(MainActivity.this,id)
                            .setCategory(Notification.CATEGORY_MESSAGE)//CATEGORY_MESSAGE这个调整种类，进度，信息，消息，等等
                            //这个icon不管用不用必须加
                            .setSmallIcon(R.drawable.ic_launcher_foreground)//设置图标

                            .setContentTitle(ContentTitle)//设置标题

                            .setContentText(ContentText)//设置正文

                            .setAutoCancel(true).build();//是否自动取消

                    manager.notify(ID,notification);//通知

                }else{
                    //如果SDK版本是小于26的
                    notification = new NotificationCompat.Builder(MainActivity.this)
                            .setSmallIcon(R.drawable.ic_launcher_foreground)//设置图标

                            .setContentTitle(ContentTitle)//设置标题

                            .setContentText(ContentText)//设置正文

                            .build();//是否自动取消

                    manager.notify(ID,notification);//通知


                }

            }
        });
    }
}